define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/tixian/index',
                    // del_url: 'user/walletrecord/del',
                    table: 'wallet_record',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'record_sn', title: '交易流水号'},
                        {field: 'user.username', title: '账号'},
                        {field: 'user.role_name', title: '角色'},
                        {field: 'remark', title: '交易备注'},
                        //{field: 'type_name', title: '交易类型'},
                        {field: 'wallet.name', title: '姓名'},
                        {
                            field: 'wallet.back_tag',
                            title: '开户行',
                            searchList: {
                                "ICBC":'中国工商银行',"CMBC":'招商银行',"ABC":'中国农业银行',"CBC":'中国建设银行',
                                "BC":'中国银行',"CMSB":'中国民生银行',"CEB":'中国光大银行',"CITIC":'中信银行',
                                "CTB":'交通银行',"CIB":'兴业银行',"SPDB":'上海浦东发展银行',"PBC":'中国人民银行',
                                "HXB":'华夏银行',"PSBC":'中国邮政储蓄银行'
                            }
                        },
                        {field: 'wallet.idcard', title: '银行卡号'},
                        {field: 'money', title: '交易金额', operate:'BETWEEN'},
                        {field: 'createtime', title: '时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'pay_status', title: '状态', searchList: {"0":'待审核',"1":'审核通过',"-1":'审核失败'}, formatter: Table.api.formatter.status},
                        {field: 'operation_name', title: '操作人', operate:false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: 'is_true',
                                    title: '通过',
                                    text:'通过',
                                    classname: 'btn btn-xs btn-success is-true',
                                },
                                {
                                    name: 'is_false',
                                    title: '失败',
                                    text:'失败',
                                    classname: 'btn btn-xs btn-danger is-false',
                                },
                                {
                                    name: 'stop',
                                    title: '停止',
                                    text:'停止',
                                    icon:'fa fa-bell-o',
                                    classname: 'btn btn-xs btn-danger stop',
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".is-false", function () { //失败
                var index = $(this).parent().parent().attr('data-index');
                var data = Table.api.getrowdata(table, index);
                var url = 'user/tixian/tixianFalse';
                layer.prompt(
                    {title: '失败(填写失败原因)', formType: 2},
                    function(text, index){
                        $.post(
                            url,
                            {id:data.id,remark:text,'money':data.money},
                            function (data) {
                                if( data.code == 0 ){
                                    Layer.close(index);
                                    Toastr.error(data.msg);
                                }else{
                                    Layer.close(index);
                                    Toastr.success(data.msg);
                                    table.bootstrapTable('refresh');
                                }
                            }
                        );

                    });
            });
            $(document).on("click", ".is-true", function () { //失败
                var index = $(this).parent().parent().attr('data-index');
                var data = Table.api.getrowdata(table, index);
                var url = 'user/tixian/tixianTrue';
                layer.confirm('确定审核通过？',{
                    btn: ['确定','取消'] //按钮
                },function () {
                    $.post(
                        url,
                        {id:data.id},
                        function (data) {
                            if( data.code == 0 ){
                                Layer.closeAll();
                                Toastr.error(data.msg);
                            }else{
                                Layer.closeAll();
                                Toastr.success(data.msg);
                                table.bootstrapTable('refresh');
                            }
                        }
                    );
                });
            });
            $(document).on("click",".stop",function () {
                var audioEle = window.parent.document.getElementById("audio");
                audioEle.src='/uploads/28.mp3';
                audioEle.pause();
                audioEle.load();
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});